package com.jufanshare.myblog.service.user;

import com.jufanshare.myblog.common.dto.AjaxRes;
import com.jufanshare.myblog.entity.User;
import com.jufanshare.myblog.service.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

/**
 * @功能：
 * @开发者： 大BUG
 * @编写时间： 2019/1/24 13:23
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Override
    public AjaxRes register(User user, HttpSession session) {
        AjaxRes res=new AjaxRes();
        User userCount=new User();
        userCount.setEmail(user.getEmail());
        long count = templateCount(userCount);
        if(count!=0){
            res.setFailMsg("此邮箱已注册，请【登录】或【找回密码】");
            return res;
        }
        String registeremailcode = (String) session.getAttribute("registeremailcode");
        String userEmailCode=user.getEmail()+user.getEmailCode();
        if(!registeremailcode.equals(userEmailCode)){
            res.setFailMsg("验证码错误！原因可能是：验证码输入错误、邮箱和验证码不匹配");
            return res;
        }
        String[] saltAndPassword = {"123","123"};//此处为了安全，原先代码已删除【密码加盐等操作】
        user.setPassword(saltAndPassword[0]);
        user.setSalt(saltAndPassword[1]);
        int randomNumber=(int)(Math.random()*10);
        user.setHeadimg("/image/userheadimg/"+(randomNumber+1)+".jpg");//1-10
        user.setRegisterdatetime(new Date());
        user.setUserstate("正常");
        insert(user,true);
        res.setRes(true);
        return res;
    }

    @Override
    public AjaxRes login(User user, HttpSession session) {
        AjaxRes res=new AjaxRes();
        User findEntity=new User();
        findEntity.setEmail(user.getEmail());
        User resutEntity = templateOne(findEntity);
        if(resutEntity==null){
            res.setFailMsg("此邮箱未注册");
            return res;
        }
        String sessionImgCode = (String) session.getAttribute("web_login_imgcode");
        if(!sessionImgCode.equals(user.getImgCode())){
            res.setFailMsg("验证码不正确");
            return res;
        }
        String passwordBySalt = "";//此处为了安全，原先代码已删除【密码加盐等操作】
        if(resutEntity.getPassword().equals(passwordBySalt)){
            res.setRes(true);
            session.setAttribute("web_userlogin_session",resutEntity);
            session.setMaxInactiveInterval(10*60);//设置  session  10分钟过期
            /**
             * 设置10分钟内退出浏览器后再次进入免登录
             */
//                Cookie c = null;
//                try {
//                    c = new Cookie("JSESSIONID", URLEncoder.encode(session.getId(), "utf-8"));
//                    c.setPath("/");
//                    //先设置cookie有效期为2天
//                    c.setMaxAge(10 * 60);
//                    response.addCookie(c);
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                }
        }else {
            res.setFailMsg("密码不正确");
        }
        return res;
    }

    @Override
    public AjaxRes reset(User user, HttpSession session) {
        AjaxRes res=new AjaxRes();
        User findEntity=new User();
        findEntity.setEmail(user.getEmail());
        User resutEntity = templateOne(findEntity);
        if(resutEntity==null){
            res.setFailMsg("此邮箱未注册");
            return res;
        }
        String sessionEmailCode = (String) session.getAttribute("resetemailcode");
        if(!sessionEmailCode.equals(user.getEmail()+user.getEmailCode())){
            res.setFailMsg("验证码错误！原因可能是：验证码输入错误、邮箱和验证码不匹配");
            return res;
        }
        String[] saltAndPassword = {"24","2342345"};//此处为了安全，原先代码已删除【密码加盐等操作】
        user.setId(resutEntity.getId());
        user.setPassword(saltAndPassword[0]);
        user.setSalt(saltAndPassword[1]);
        int i = updateTemplateById(user);
        if(i!=0){
            res.setRes(true);
        }else {
            res.setFailMsg("修改失败，请稍后再试");
        }
        return res;
    }
}
